package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.dtos.ChannelDTO;
import com.heima.admin.mapper.ChannelMapper;
import com.heima.admin.pojos.AdChannel;
import com.heima.admin.service.ChannelService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Description:
 */
@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, AdChannel> implements ChannelService {

    @Override
    public ResponseResult findByNameAndPage(ChannelDTO dto) {
        //1、校验参数
        if(dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"参数错误");
        }
        dto.checkParam();
        //2、条件查询（使用lambda，下面构造器写条件时更加直观)
        LambdaQueryWrapper<AdChannel> wrapper = Wrappers.lambdaQuery();

        //  判断条件 (isNotBlank 检查严格，可以去除空格后进行检查)
        if(StringUtils.isNotBlank(dto.getName())){
            wrapper.like(AdChannel::getName,dto.getName());
        }

        if(dto.getStatus() != null){
            wrapper.eq(AdChannel::getStatus,dto.getStatus());
        }
        // 排序在分页之前，分页是查询语句后，再进行的
        wrapper.orderByAsc(AdChannel::getOrd);
        //  封装分页查询
        Page<AdChannel> page = new Page<>(dto.getPage(),dto.getSize());
        //  封装条件参数  name，status，排序，执行查询
        //  继承了ServiceImpl<>
        IPage<AdChannel> iPage = this.page(page, wrapper);
        //3、执行查询，封装返回结果，PageResponseResult
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),iPage.getTotal(),
                iPage.getRecords());

        return responseResult;
    }

    @Override
    public ResponseResult insert(AdChannel channel) {
        if(!StringUtils.isNotBlank(channel.getName())){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称不能为空");
        }
        if(channel.getName().length()  > 10){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"名称长度不能大于10");
        }
        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, channel.getName()));
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道名称已存在");
        }
        channel.setCreatedTime(new Date());
        save(channel);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {

        if(adChannel == null || adChannel.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"id不能为空");
        }

        AdChannel channel = getById(adChannel.getId());
        if(channel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道不存在");
        }

        if(StringUtils.isNotBlank(adChannel.getName()) && !channel.getName().equals(adChannel.getName())){
            //检验输入的名称是否存在
            int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, adChannel.getName()));
            if(count > 0){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道名称已存在");
            }
        }
        updateById(adChannel);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        if(id == null || id < 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"id无效");
        }
        AdChannel channel = this.getById(id);
        /*int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getId, id));
        if(count == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"记录不存在");
        }*/
        if(channel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"记录不存在");
        }
        //AdChannel adChannel = this.getOne(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getId, id));
        if(channel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_ALLOW,"当前数据有效不可删除");
        }
        this.removeById(id);
        return ResponseResult.okResult();
    }


}
